//
// Created by 施旗坤 on 2025/11/7.
//
#include "admin_function.h"
int clear_buffer1() {
    int c;
    while ((c = getchar()) != '\n' && c != EOF);
}

int addbook(){
    book_information b;
    printf("请输入要添加的图书名字: ");
    scanf("%99s", b.book_name);
    clear_buffer1();  // 清理缓冲区残留字符
    printf("请输入要添加的图书价格: ");
    if (scanf("%lf", &b.price) != 1 || b.price < 0) {
        printf("输入错误：请输入有效的非负价格\n");
        clear_buffer1();
        return -1;
    }
    clear_buffer1();

    printf("请输入要添加的图书出版社: ");
    scanf("%99s", b.publish);
    clear_buffer1();

    printf("请输入要添加的图书作者: ");
    scanf("%99s", b.author);
    clear_buffer1();

    printf("请输入要添加的图书日期: ");
    scanf("%19s", b.publish_date);
    clear_buffer1();

    printf("请输入要添加的图书种类编号: ");
    if (scanf("%d", &b.category_id) != 1 || b.category_id <= 0||b.category_id>3) {
        printf("输入错误：请输入有效的整数编号(1-3)\n");
        clear_buffer1();
        return -1;
    }
    clear_buffer1();

    printf("请输入要添加的图书数量: ");
    if (scanf("%d", &b.stock) != 1 || b.stock <= 0) {
        printf("输入错误：请输入有效的正整数数量\n");
        clear_buffer1();
        return -1;
    }
    clear_buffer1();

    const char * cleck_sql="select count (*) from book where book_name=?";
    sqlite3_stmt * cleck_stmt;
    int rc= sqlite3_prepare_v2(db,cleck_sql,-1,&cleck_stmt,NULL);
    if (rc!=SQLITE_OK){
        fprintf(stderr,"预编译检查语句失败%s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_bind_text(cleck_stmt,1,b.book_name,-1,SQLITE_TRANSIENT);
    int row_count;
    if (sqlite3_step(cleck_stmt)==SQLITE_ROW) {
        row_count = sqlite3_column_int(cleck_stmt, 0);
        if (row_count > 0) {
            printf( "图书《%s》已存在，无需重复添加\n", b.book_name);
            sqlite3_finalize(cleck_stmt);
            return -1;
        }
    }else{
        fprintf(stderr, "校验编号失败：%s\n", sqlite3_errmsg(db));
        sqlite3_finalize(cleck_stmt);
        return -1;
    }
    sqlite3_finalize(cleck_stmt);
    const char * insert_sql="insert into book (book_name,price,publish,author,publish_date,category_id,stock) values "
                            "(?,?,?,?,?,?,?)";
    sqlite3_stmt * insert_stmt;
    rc= sqlite3_prepare_v2(db,insert_sql,-1,&insert_stmt,NULL);
    if (rc!=SQLITE_OK){
        fprintf(stderr,"预编译添加语句失败%s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_bind_text(insert_stmt,1,b.book_name,-1,SQLITE_TRANSIENT);
    sqlite3_bind_double(insert_stmt,2,b.price);
    sqlite3_bind_text(insert_stmt,3,b.publish,-1,SQLITE_TRANSIENT);
    sqlite3_bind_text(insert_stmt,4,b.author,-1,SQLITE_TRANSIENT);
    sqlite3_bind_text(insert_stmt,5,b.publish_date,-1,SQLITE_TRANSIENT);
    sqlite3_bind_int(insert_stmt,6,b.category_id);
    sqlite3_bind_int(insert_stmt,7,b.stock);
    rc= sqlite3_step(insert_stmt);
    if (rc!=SQLITE_DONE){
        fprintf(stderr,"添加图书失败：%s\n", sqlite3_errmsg(db));
        sqlite3_finalize(insert_stmt);
        return -1;
    }
    sqlite3_finalize(insert_stmt);
    printf("图书《%s》添加成功\n",b.book_name);
    return 0;
}
int deletebook(){
    book_information b;
    printf("请输入删除图书名字:\n");
    scanf("%s",b.book_name);
    clear_buffer1();
    const char * select_sql="select count(*) from book where book_name=?";
    sqlite3_stmt * select_stmt;
    int rc= sqlite3_prepare_v2(db,select_sql,-1,&select_stmt,NULL);
    if (rc!=SQLITE_OK){
        fprintf(stderr,"预编译检查语句失败%s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_bind_text(select_stmt,1,b.book_name,-1,SQLITE_TRANSIENT);
    int row=0;
    if (sqlite3_step(select_stmt)==SQLITE_ROW){
        row= sqlite3_column_int(select_stmt,0);
    }
    sqlite3_finalize(select_stmt);
    if (row==0){
        printf("该图书《%s》不存在!\n", b.book_name);
        return -1;
    }
    const char * delete_sql="delete from book where book_name=?";
    sqlite3_stmt * delete_stmt;
    int rc1= sqlite3_prepare_v2(db,delete_sql,-1,&delete_stmt,NULL);
    if (rc1!=SQLITE_OK){
        fprintf(stderr,"预编译删除语句失败: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_bind_text(delete_stmt,1,b.book_name,-1,SQLITE_TRANSIENT);
    int rc2= sqlite3_step(delete_stmt);
    if (rc2!=SQLITE_DONE){
        fprintf(stderr,"删除图书失败：%s\n", sqlite3_errmsg(db));
        sqlite3_finalize(delete_stmt);
        return -1;
    }
    printf("删除图书成功\n");
    return 0;
}

int updatebook(){
    book_information b;
    int origin_book_id=-1;
    printf("请输入要修改的图书名字: \n");
    scanf("%s",b.book_name);
    clear_buffer1();
    const char * select_sql="select id from book where book_name =?";
    sqlite3_stmt * select_stmt;
    int rc= sqlite3_prepare_v2(db,select_sql,-1,&select_stmt,NULL);
    if (rc!=SQLITE_OK){
        fprintf(stderr,"预编译查询失败%s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_bind_text(select_stmt,1,b.book_name,-1,SQLITE_TRANSIENT);
    int row_count=0;

    while (sqlite3_step(select_stmt)==SQLITE_ROW){
        row_count++;
        int id= sqlite3_column_int(select_stmt,0);
        printf("ID:%d\n",id);
        if (row_count==1){
            origin_book_id=id;
        }
    }
    sqlite3_finalize(select_stmt);
    if (row_count==0){
        printf("要修改的图书不存在%s\n",b.book_name);
        return -1;
    }
    if (row_count > 1) {
        printf("请输入要修改的图书ID：\n");
        if (scanf("%d", &origin_book_id) != 1 || origin_book_id <= 0) {
            printf( "输入错误：无效的ID\n");
            while (getchar() != '\n');
            return -1;
        }
    }
    printf("请输入新的图书编号\n");
    if (scanf("%d",&b.id)!=1||b.id<=0){
        printf("输入错误：请输入有效的图书编号");
        while (getchar()!='\n');
            return -1;
    }
    // 在输入新ID后添加
    const char *check_id_sql = "select count(*) from book where id = ?";
    sqlite3_stmt *check_stmt;
    int check_rc = sqlite3_prepare_v2(db, check_id_sql, -1, &check_stmt, NULL);
    if (check_rc != SQLITE_OK) {
       fprintf(stderr, "校验新ID失败：%s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_bind_int(check_stmt, 1, b.id);
    check_rc = sqlite3_step(check_stmt);
    if (check_rc == SQLITE_ROW && sqlite3_column_int(check_stmt, 0) > 0) {
        printf( "新ID %d 已存在，请更换\n", b.id);
        sqlite3_finalize(check_stmt);
        return -1;
    }
    sqlite3_finalize(check_stmt);
    printf("请输入新的图书价格: \n");
    if (scanf("%lf",&b.price)!=1||b.price<=0){
        printf("输入错误：请输入有效的图书价格");
        while (getchar()!='\n');
            return -1;
    }
    printf("请输入新的图书出版社: \n");
    scanf("%s",b.publish);
    printf("请输入新的图书作者: \n");
    scanf("%s",b.author);
    printf("请输入新的图书日期: \n");
    scanf("%s",b.publish_date);
    printf("请输入新的图书种类编号\n");
    if (scanf("%d",&b.category_id)!=1||b.category_id<=0){
        printf("输入错误：请输入有效的图书种类编号\n");
        while (getchar()!='\n');
            return -1;
    }
    printf("请输入新的图书数量: \n");
    if (scanf("%d",&b.stock)!=1||b.stock<=0){
        printf("输入错误：请输入有效的图书数量\n");
        while (getchar()!='\n');
            return -1;
    }
    const char * update_sql="update book set "
                           " book_name=?,"
                           " id=?,"
                           " price=?,"
                           " publish=?,"
                           " author=?,"
                           " publish_date=?,"
                           " category_id=?,"
                           " stock=? "
                           " where id=?";
    sqlite3_stmt * update_stmt;
    rc= sqlite3_prepare_v2(db,update_sql,-1,&update_stmt,NULL);
    if (rc!=SQLITE_OK){
        fprintf(stderr,"预编译修改失败%s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_bind_text(update_stmt,1,b.book_name,-1,SQLITE_TRANSIENT);
    sqlite3_bind_int(update_stmt,2,b.id);
    sqlite3_bind_double(update_stmt,3,b.price);
    sqlite3_bind_text(update_stmt,4,b.publish,-1,SQLITE_TRANSIENT);
    sqlite3_bind_text(update_stmt,5,b.author,-1,SQLITE_TRANSIENT);
    sqlite3_bind_text(update_stmt,6,b.publish_date,-1,SQLITE_TRANSIENT);
    sqlite3_bind_int(update_stmt,7,b.category_id);
    sqlite3_bind_int(update_stmt,8,b.stock);
    sqlite3_bind_int(update_stmt,9,origin_book_id);
    rc= sqlite3_step(update_stmt);
    if (rc!=SQLITE_DONE){
        fprintf(stderr,"修改图书失败%s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_finalize(update_stmt);
    printf("修改图书成功\n");
    return 0;
}